package src.NPCs;

import src.Util.*;
import src.Main;

public class NPCUpdateBlock
{
    public NPCUpdateBlock()
    {
    }

    public void writeMask(Stream str, int updateMask)
    {
        if(str == null)
        {
            return;
        }
        if(updateMask >= 0x100)
        {
            updateMask |= 0x40;
            str.writeByte(updateMask & 0xFF);
            str.writeByte(updateMask >> 8);
        }
        else
        {
            str.writeByte(updateMask);
        }
    }

    public void appendNPCSpeakText(NPC n, Stream str)
    {
        if(n == null || str == null)
        {
            return;
        }
        str.writeString(n.speakText);
    }

    public void appendAnimationRequest(NPC n, Stream str)
    {
        if(n == null || str == null)
        {
            return;
        }
        str.writeWordBigEndian(n.animRequest);
        str.writeByte(n.animDelay);
    }

    public void appendFaceCoords(NPC n, Stream str)
    {
        if(n == null || str == null)
        {
            return;
        }
        str.writeWordBigEndian(n.faceCoordsX);
        str.writeWordBigEndian(n.faceCoordsY);
    }

    public void appendGraphicsRequest(NPC n, Stream str)
    {
        if(n == null || str == null)
        {
            return;
        }
        str.writeWord(n.gfxRequest);
        str.writeDWord(n.gfxDelay);
    }

    public void appendHit1(NPC n, Stream str)
    {
        if(n == null || str == null)
        {
            return;
        }
        str.writeByteC(n.hitDiff1);
        if(n.posionHit1 == 0)
        {
            if(n.hitDiff1 > 0)
            {
                str.writeByteS(1);
            }
            else
            {
                str.writeByteS(0);
            }
        }
        else
        {
            str.writeByteS(2);
        }
        str.writeByteS(getCurrentHP(n.currentHP, n.maxHP, 100));
        str.writeByteC(100);
    }

    private int getCurrentHP(int i, int i1, int i2)
    {
        double x = (double)i / (double)i1;
        return (int)Math.round(x * i2);
    }
}